import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
  state: () => ({
    name: '',
    age: 0,
    email: '',
    isLoggedIn: false
  }),
  
  getters: {
    userInfo: (state) => ({
      name: state.name,
      age: state.age,
      email: state.email
    }),
    isAdult: (state) => state.age >= 18
  },
  
  actions: {
    login(userInfo) {
      this.name = userInfo.name
      this.age = userInfo.age
      this.email = userInfo.email
      this.isLoggedIn = true
    },
    
    logout() {
      this.name = ''
      this.age = 0
      this.email = ''
      this.isLoggedIn = false
    },
    
    updateProfile(profile) {
      this.name = profile.name || this.name
      this.age = profile.age || this.age
      this.email = profile.email || this.email
    }
  },
  
  // 持久化配置
  persist: {
    key: 'user-store',
    storage: localStorage
  }
})